﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bigscreen_ai_demo
{
    public class PYHelper
    {
        public  static string Run(string filePath,string args)
        {

            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName =Config.PythonPath,
                WorkingDirectory = Path.GetDirectoryName(filePath),
                Arguments = filePath + " " + args,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            using (Process process = new Process { StartInfo = startInfo })
            {


                process.Start();

                // Begin asynchronous reading of the output stream.  
                //process.BeginOutputReadLine();
                //process.BeginErrorReadLine();

                string output = process.StandardOutput.ReadToEnd();//读取进程的标准输出流到结束，通常包括命令的执行结果。
                string errors = process.StandardError.ReadToEnd();//读取进程的错误输出流到结束，捕获执行过程中可能发生的错误
                if (!string.IsNullOrEmpty(errors))
                {
                    throw new Exception(errors);
                }
                // Wait for the process to exit.  
                process.WaitForExit();
                return output;
            }

        }

//        public static void RuntimeRun()
//        {

//            string pathToVirtualEnv = @"C:\Program Files\python";
//            Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process);
//            Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
//            Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib;{pathToVirtualEnv}\\DLLs", EnvironmentVariableTarget.Process);

//            Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python312.dll");
//            PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe");
//PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);
//            PythonEngine.Initialize();

//            using (Py.GIL())
//            {
//                dynamic py = Py.Import("load.py"); // 导入Python模块
//                var res = py.run("0.0007", "-0.006", "284.8125", "307", "96.140625", "57.71875", "6.5", "0.040993");
//                //py.your_python_function("0.0007 -0.006 284.8125 307 96.140625 57.71875 6.5 0.040993"); // 调用Python函数

//            }
//        }
    }
}
